\set VERBOSITY terse
\set HIVE_SERVER         `echo \'"$HIVE_SERVER"\'`
\set HIVE_CLIENT_TYPE    `echo \'"$CLIENT_TYPE"\'`
\set HIVE_PORT           `echo \'"$HIVE_PORT"\'`
\set HIVE_USER           `echo \'"$HIVE_USER"\'`
\set HIVE_PASSWORD       `echo \'"$HIVE_PASSWORD"\'`
\set AUTH_TYPE           `echo \'"$AUTH_TYPE"\'`

\c contrib_regression

CREATE EXTENSION IF NOT EXISTS hdfs_fdw;

CREATE OR REPLACE FUNCTION show_details(host TEXT, port TEXT, client_type TEXT, auth_type TEXT)
RETURNS int AS $$
DECLARE
  ext TEXT;
  srv TEXT;
  sopts TEXT;
BEGIN
  SELECT e.fdwname, srvname, array_to_string(s.srvoptions, ',')
    INTO ext, srv, sopts
    FROM pg_foreign_data_wrapper e LEFT JOIN pg_foreign_server s ON e.oid = s.srvfdw
    WHERE e.fdwname = 'hdfs_fdw'
    ORDER BY 1, 2, 3;

  raise notice 'Extension            : %', ext;
  raise notice 'Server               : %', srv;

  IF strpos(sopts, client_type) <> 0 AND strpos(sopts, auth_type) <> 0 THEN
    raise notice 'Server_Options       : matched';
  END IF;

  return 1;
END;
$$ language plpgsql;

-- Create Server with different client_type & auth_type options
-- Test client type SPARK with NOSASL
CREATE SERVER hdfs_server FOREIGN DATA WRAPPER hdfs_fdw
 OPTIONS(host :HIVE_SERVER, port :HIVE_PORT, client_type 'spark', auth_type 'NOSASL');
SELECT show_details(:HIVE_SERVER, :HIVE_PORT, 'spark', 'NOSASL');
DROP SERVER hdfs_server;
-- Test client type SPARK with LDAP
CREATE SERVER hdfs_server FOREIGN DATA WRAPPER hdfs_fdw
 OPTIONS(host :HIVE_SERVER, port :HIVE_PORT, client_type 'spark', auth_type 'LDAP');
SELECT show_details(:HIVE_SERVER, :HIVE_PORT, 'spark', 'LDAP');
DROP SERVER hdfs_server;
-- Test client type HIVE with NOSASL
CREATE SERVER hdfs_server FOREIGN DATA WRAPPER hdfs_fdw
 OPTIONS(host :HIVE_SERVER, port :HIVE_PORT, client_type 'hiveserver2', auth_type 'NOSASL');
SELECT show_details(:HIVE_SERVER, :HIVE_PORT, 'hiveserver2', 'NOSASL');
DROP SERVER hdfs_server;
-- Test client type HIVE with LDAP
CREATE SERVER hdfs_server FOREIGN DATA WRAPPER hdfs_fdw
 OPTIONS(host :HIVE_SERVER, port :HIVE_PORT, client_type 'hiveserver21', auth_type 'LDAP');
SELECT show_details(:HIVE_SERVER, :HIVE_PORT, 'hiveserver2', 'LDAP');
DROP SERVER hdfs_server;

-- Verify that error message is displayed when
-- invalid value is used with client_type Parameter.
CREATE SERVER hdfs_server FOREIGN DATA WRAPPER hdfs_fdw
 OPTIONS(host :HIVE_SERVER, port :HIVE_PORT, client_type 'invalid', auth_type :AUTH_TYPE);
CREATE USER MAPPING FOR public SERVER hdfs_server
 OPTIONS (username :HIVE_USER, password :HIVE_PASSWORD);
CREATE FOREIGN TABLE dept (
    deptno          INTEGER,
    dname           VARCHAR(14),
    loc             VARCHAR(13)
)
SERVER hdfs_server OPTIONS (dbname 'fdw_db', table_name 'dept');
SELECT * FROM dept ORDER BY deptno;
DROP FOREIGN TABLE dept;
DROP USER MAPPING FOR public SERVER hdfs_server;
DROP SERVER hdfs_server;

-- Verify that when auth_type is used then user name must be
-- defined while creating user mapping RM#42047
CREATE SERVER hdfs_server FOREIGN DATA WRAPPER hdfs_fdw
 OPTIONS(host :HIVE_SERVER, port :HIVE_PORT, client_type :HIVE_CLIENT_TYPE, auth_type :AUTH_TYPE);
CREATE USER MAPPING FOR public SERVER hdfs_server
 OPTIONS (password :HIVE_PASSWORD);
CREATE FOREIGN TABLE dept (
    deptno          INTEGER,
    dname           VARCHAR(14),
    loc             VARCHAR(13)
)
SERVER hdfs_server OPTIONS (dbname 'fdw_db', table_name 'dept');
SELECT * FROM dept ORDER BY deptno;
DROP FOREIGN TABLE dept;
DROP USER MAPPING FOR public SERVER hdfs_server;
DROP SERVER hdfs_server;

-- When client_type is specified as spark then analyze
-- command should pass RM # 41527
CREATE SERVER hdfs_server FOREIGN DATA WRAPPER hdfs_fdw
 OPTIONS(host :HIVE_SERVER, port :HIVE_PORT, client_type :HIVE_CLIENT_TYPE, auth_type :AUTH_TYPE);
CREATE USER MAPPING FOR public SERVER hdfs_server
 OPTIONS (username :HIVE_USER, password :HIVE_PASSWORD);
CREATE FOREIGN TABLE dept (
    deptno          INTEGER,
    dname           VARCHAR(14),
    loc             VARCHAR(13)
)
SERVER hdfs_server OPTIONS (dbname 'fdw_db', table_name 'dept');

-- Since ANALYZE is supported only with spark, below
-- procedure will run analyze only for client type as spark
-- and exit silently if it is executed without any error.
-- Other client types are simply ignored.
CREATE OR REPLACE FUNCTION execute_based_client (client_type VARCHAR(70))
 RETURNS void AS $$
DECLARE
 d_sql text;
BEGIN
 IF client_type = 'spark' THEN
  d_sql := 'ANALYZE dept';
   EXECUTE d_sql;
 END IF;
END;
$$ LANGUAGE plpgsql;

SELECT execute_based_client(:HIVE_CLIENT_TYPE);

-- Test client_type
-- set to hiveserver1, should error when querying
ALTER SERVER hdfs_server OPTIONS (SET client_type 'hiverserver1');
SELECT * FROM dept;

-- cleanup
DROP FUNCTION execute_based_client (character varying);
DROP FOREIGN TABLE dept;
DROP USER MAPPING FOR public SERVER hdfs_server;
DROP SERVER hdfs_server;
DROP EXTENSION hdfs_fdw;
